<?php
include('class.db.php');
class Upload
{
  private $_errors;		// Errors worden in een array opgeslagen en op het laatste getoond

  private $_rootDir;    // Main folder voor de uploads, die we "uploads" zullen noemen (kei-origineel !!)

  private $_fileDest;    //Direct file location on server

  private $_folderName; //Folder name randomly generated
  private $_folderPath;

  private $_fileName;
  private $_fileTemp;
  private $_fileSize;
  private $_fileType;
  private $_fileExt;


  public function __construct()
  {
    $this->_errors = array();

    $this->_fileName  = $_FILES['file']['name'];
    $this->_fileTemp  = $_FILES['file']['tmp_name'];
    $this->_fileSize  = $_FILES['file']['size'] / 1024;
    $this->_fileType  = $_FILES['file']['type'];
	
	if(ereg(".([^\.]+)$",$this->_fileName,$r)){
		$this->_fileExt = strtolower($r[1]);
	}
	else{
		$this->_fileExt = '';
	}

    $this->_rootDir   = '../uploads/';

    $this->_folderName = $_FILES['file']['name'];
    $this->_folderPath = $this->_rootDir.$this->_folderName.'/';

    // $this->_imgName    = $this->_fileName;
    $this->_fileDest    = $this->_folderPath.$this->_fileName;           // uploads/76sd7/798234.jpg

  }

  public function uploadProcess()
  {
    try
    {
      if(!$this->isfileValid())
         throw new Exception('Invalid File Type/Size');
      if(!$this->saveFile())
         throw new Exception('Upload process not succesful');

    return true;
    }
    catch(Exception $e)
    {
      $this->_errors[] = $e->getMessage();
      return false;
    }
  }

  public function saveFile()
  {
    try
    {
      if(!$this->createFileDir())
         throw new Exception('Could not create imageDir');

      if(!$this->moveUploadedFile())
         throw new Exception('Could not move file');

      if(!$this->saveToDatabase())
         throw new Exception('Could not save file to database');

      return true;
    }
    catch(Exception $e)
    {
      $this->errors[] = $e->getMessage();
	  $this->removeFileDir();
    }
  return false;
  }

  public function createFileDir()
  {
    if(!is_dir($this->_rootDir))
      mkdir($this->_rootDir,0777);

    if(!is_dir($this->_folderPath))
      {
        mkdir($this->_folderPath,0777);
        return true;
      }
    else
      return false;
  }

  public function removeFileDir()
  {
    rmdir($this->_folderPath);

    if(is_dir($this->_folderPath)){ return false; }
	else{ return true; }
  }

  public function moveUploadedFile()
  {
    @move_uploaded_file($this->_fileTemp,$this->_fileDest);
    @chmod($this->_fileDest,octdec(0755));

    return file_exists($this->_fileDest)? 1 : 0;
  }

  public function isFileValid()
  {
    if(($this->_fileExt != "jpg" && $this->_fileExt != "jpeg" && $this->_fileExt != "gif" && $this->_fileExt != "pdf" && $this->_fileExt != "xls" && $this->_fileExt != "xlsx" && 
		$this->_fileExt != "doc" && $this->_fileExt != "docx") || ($this->_fileSize > 2048 /* =2Mb */ )){ 
			return false;
	}
	else{ return true; }
  }

  public function saveToDatabase()
  {
    //Database Connection Data
	$db = new db();
	$sql = "INSERT INTO oplossingen (name,type,description,size) VALUES ('{$this->_fileName}','{$this->_fileExt}','een upload','{$this->_fileSize}')";
    $database = "vdab";
	
	$result = $db->query($database, $sql);
	
	if($result){
		return true;
	}else {
		return false;
	}

	
  }

  public function showErrors()
  {
    echo "<h3>Errors</h3>";

    foreach($this->_errors as $key=>$value)
      echo $value."<br>";
  }

 } 
 
 ?>